package work.mediway.bi.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mediway.hos.app.core.util.SecurityUtils;
import com.mediway.hos.common.exception.GlobalException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import work.mediway.bi.constant.BiDesignConstant;
import work.mediway.bi.mapper.DesignTemplateMapper;
import work.mediway.bi.model.dto.BiDesignTemplateDTO;
import work.mediway.bi.model.entity.DesignTemplate;
import work.mediway.bi.model.vo.BiDesignTemplateVO;
import work.mediway.bi.service.DesignTemplateService;

import java.util.List;
/**
  *@version
  *@description      
  *@author          bovane <bovane.ch@gmail.com>
  *@create          2023-06-15 14:43
  */  

@Service
@Slf4j
public class DesignTemplateServiceImpl extends ServiceImpl<DesignTemplateMapper, DesignTemplate> implements DesignTemplateService{

    @Override
    public IPage<BiDesignTemplateVO> getBiDesignTemplate(BiDesignTemplateDTO biDesignTemplateDTO) {
        IPage<DesignTemplate> page = new Page<>(biDesignTemplateDTO.getCurrent(),biDesignTemplateDTO.getSize());
        List<DesignTemplate> result = this.baseMapper.selectBiDesignTemplate(page,biDesignTemplateDTO);

        IPage<BiDesignTemplateVO> page1 = new Page<>(biDesignTemplateDTO.getCurrent(),biDesignTemplateDTO.getSize());
        List<BiDesignTemplateVO> biDesignTemplateVOS = BiDesignTemplateVO.convert(result);
        log.info(result.toString());
        page1.setRecords(biDesignTemplateVOS);
        return page1;
    }

    @Override
    public Boolean addBiDesignTemplate(DesignTemplate designTemplate) {
        // 设置新增默认启用状态，创建人以及时间等
        if (designTemplate.getActived() == null) {
            designTemplate.setActived(Boolean.TRUE);
        }
        // 查看UUID是否重复
        DesignTemplate one = this.getOne(new LambdaQueryWrapper<DesignTemplate>()
                .eq(DesignTemplate::getTemplateCode, designTemplate.getTemplateCode()));
        if (ObjectUtil.isNotEmpty(one)) {
            throw new GlobalException(String.format("当前模板UUID已经存在【{}】", designTemplate.getTemplateCode()));
        }

        designTemplate.setCreateBy(SecurityUtils.getLoginUser().getUserName());
        designTemplate.setCreateTime(DateUtil.date());
        return this.save(designTemplate);
    }

    @Override
    public Boolean updateBiDesignTemplate(DesignTemplate designTemplate) {
        designTemplate.setUpdateBy(SecurityUtils.getLoginUser().getUserName());
        designTemplate.setUpdateTime(DateUtil.date());
        return this.updateById(designTemplate);
    }

    @Override
    public void deleteBiDesignTemplate(String ids) {
        List<String> id = StrUtil.split(ids, BiDesignConstant.DEFAULT_SPLIT);
        this.removeBatchByIds(id);
    }
}
